class OrderManager
  
  @@AdminAccount=Account.find_by_user_id("3")
  
  def initialize
  end
  
  def self.paid_to_third(order)
    if order
      begin
        Account.transaction do          
          AccountManager.transfer(:money=>order.order_price_sum,
            :sender=>Account.find_by_user_id(order.user_id),
            :receiver=>@@AdminAccount,
            :order=>order)  
          order.save!                                
        end
      rescue ActiveRecord::RecordNotSave
        return false
      else
        return true
      end   
    end
  end
    
  def self.product_sent(order)
    if order.status==Order.all_status['buyer_paid']
      begin
        Order.transaction do
          order.update_attributes!(:status=>Order.all_status['product_sent'])       
        end
      rescue ActiveRecord::RecordNotSaved
        return false
      else
        return true
      end
    else
      return false
    end
  end
    
  def self.paid_to_seller(order)
    if order.status==Order.all_status['product_sent']
      begin
        Order.transaction do
          AccountManager.transfer(:money=>order.order_price_sum,
            :sender=>@@AdminAccount,
            :receiver=>order.shop.user.account,
            :order=>order)  
          order.update_attributes!(:status=>Order.all_status['buyer_confirm'])        
        end
      rescue ActiveRecord::RecordNotSaved
        return false
      else
        return true
      end
    else
      return false
    end
  end
    
  def self.out_of_sell(order)
    if order.status==Order.all_status['product_sent']
      begin
        Order.transaction do
          order.update_attributes!(:status=>Order.all_status['out_of_sell'])
        end
      rescue ActiveRecord::RecordNotSaved
        return false
      else
        return true
      end
    else
      return false
    end
  end
    
  def self.paid_to_buyer(order)
    if order.status==Order.all_status['product_returned']
      if order
        begin
          Account.transaction do          
            AccountManager.transfer(:money=>order.order_price_sum,
              :sender=>@@AdminAccount,
              :receiver=>Account.find_by_user_id(order.user_id),
              :order=>order)  
            order.save!                                
          end
        rescue ActiveRecord::RecordNotSave
          return false
        else
          return true
        end   
      end
    else
      return false
    end
  end
  
  
end
